import pymysql


class DBUitl:
    conn = None

    @classmethod
    def __conn_get(cls):
        if cls.conn is None:
            cls.conn = pymysql.connect(host="211.103.136.244", port=7061, user="student", password="iHRM_student_2021",
                                       database="ihrm", charset="utf8")
        return cls.conn

    @classmethod
    def __conn_close(cls):
        if cls.conn is not None:
            cls.conn.close()
            cls.conn = None

    @classmethod
    def get_one(cls, sql):
        cursor = None
        try:
            conn = DBUitl.__conn_get()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
            return res
        except Exception as err:
            print("异常结果:", err)
        finally:
            cursor.close()
            cls.__conn_close()

    @classmethod
    def uid_db(cls, sql):
        cursor = None
        try:
            conn = DBUitl.__conn_get()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
        except Exception as err:
            print("异常结果:", err)
        finally:
            cursor.close()
            cls.__conn_close()
